package com.example.Servlet;

import com.example.Service.UserService;
import com.example.pojo.User;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "EditUserServlet", value = "/EditUserServlet")
public class EditUserServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setContentType("text/html");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        BufferedReader br = new BufferedReader(req.getReader());
        StringBuffer sbuffer = new StringBuffer();
        String s = "";
        while ((s = br.readLine()) != null) {
            sbuffer.append(s);
        }
        System.out.println(sbuffer.toString());
        Gson gson = new Gson();

        User user = gson.fromJson(sbuffer.toString(),User.class);
        UserService userService = new UserService();

        int flag=userService.EditUser(user);
        Map<String,Object> map=new HashMap<>();
        if (flag>0) {
            map.put("code", 200);
            map.put("msg", "编辑成功");
        } else {
            map.put("code", 500);
            map.put("msg", "编辑失败");
        }
        String json = gson.toJson(map);
        System.out.println(json);
        resp.getWriter().write(json);
    }
    public void destroy() {
    }
}